package com.zengqingfa.demo.listener;

import com.zengqingfa.demo.event.MyEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author zengqingfa
 * @date 2019/11/22 13:28
 * @description
 * @email zengqingfa_java@163.com
 * <p>
 * 方式四：，该类无需实现ApplicationListener接口，使用@EventListener装饰具体方法
 *
 * 四种方式输出结果：
 *
 * 2019-11-22 13:43:04.754  INFO 445140 --- [  restartedMain] c.zengqingfa.demo.listener.MyListener3   : com.zengqingfa.demo.listener.MyListener3监听到事件源：发布事件.
 * 2019-11-22 13:43:04.754  INFO 445140 --- [  restartedMain] c.zengqingfa.demo.listener.MyListener4   : com.zengqingfa.demo.listener.MyListener4监听到事件源：发布事件.
 * 2019-11-22 13:43:04.754  INFO 445140 --- [  restartedMain] c.zengqingfa.demo.listener.MyListener2   : com.zengqingfa.demo.listener.MyListener2监听到事件源：发布事件.
 * 2019-11-22 13:43:04.754  INFO 445140 --- [  restartedMain] c.zengqingfa.demo.listener.MyListener1   : com.zengqingfa.demo.listener.MyListener1监听到事件源：发布事件.
 */
@Component
public class MyListener4 {

    Logger logger = LoggerFactory.getLogger(MyListener4.class);

    @EventListener
    public void onApplicationEvent(MyEvent event) {
        logger.info(String.format("%s监听到事件源：%s.", MyListener4.class.getName(), event.getSource()));
    }
}
